<?php

function translate($params,& $smarty)
{
	return PXFLang::get($params['t'],$smarty->_plugins['function']['_'][1]);
}

function datef($params,& $smarty)
{
	return date(PXFConfig::DISPLAY_DATE,strtotime($params['d']));
}

function build_url($params,& $smarty)
{


	if(isset($params['controller'])) 
	{
		$controller = $params['controller'];
		unset($params['controller']);
	}
	else
	{
		$controller = PXFConfig::DEFAULT_CONTROLLER;
	}

	if(isset($params['action']))
	{
		$action = $params['action'];
		unset($params['action']);
	}     
	else
	{
		$action = 'index';
	}

	if(isset($params['lang']))
	{
		$lang = $params['lang'];
		unset($params['lang']);
	}     
	else
	{
		$lang = PXFInstance::$lang;
	}


	$result  = 'http://'.PXFConfig::HOSTNAME.PXFConfig::REQUEST_URI ."/$lang/$controller/$action";

	if(array_key_exists('args',$params))
	{
		if(is_array($params['args']))
		{
			foreach($params['args'] as $key => $value);
				$result .= "/$key/$value";
		}
		unset($params['args']);
	}


	if(count($params) > 0)
	{
		foreach($params as $key => $value);
			$result .= "/$key/$value";
		unset($params);
	}
		
	return $result;
}

function pxf_json_encode($array)
{
	$tmpArray = array();
  $result   = '';

	if(!empty($array) && (array_keys($array) !== range(0, count($array) - 1)))
	{
  	// Associative array
    $result = '{';
    foreach ($array as $key => $value)
		{
			if(is_array($array[$key]))
			{
				$tmpArray[] =  '"'.$key.'":'. pxf_json_encode($value);
			}
			else
			{
    		$key = (string) $key;
      	$tmpArray[] = sprintf('"%s":"%s"',$key,$value);
			}
		}
    $result .= implode(',', $tmpArray);
    $result .= '}';
  } 
	else
	{
  	// Indexed array
  	$result = '[';
    $length = count($array);
    for ($i = 0; $i < $length; $i++)
		{
			if(is_array($array[$i]))
			{
    		$tmpArray[] = pxf_json_encode($array[$i]);
			}
			else
			{
    		$tmpArray[] = $array[$i];
			}

    }
    $result .= implode(',', $tmpArray);
    $result .= ']';
  }
	return $result;
}
?>
